﻿#region Copyright (C) 2011-2012, Cory Cissell

// This is free software: you can redistribute it and/or modify
// it under the terms of the Apache License 2.0 (Apache)
// as published by the Free Software Foundation.
// It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the Apache License 2.0 for more details <http://www.apache.org/licenses/>

#endregion

using EnvDTE;

namespace VsExt.TaskbarBuildProgress
{
    internal class SolutionEventListener : BaseEventListener
    {
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Reference to the Projects SolutionEvents.
        /// </summary>
        /// <remarks>
        /// The SolutionEvents object can go out of scope and be garbage collected before the solution is closed. 
        /// To retain a reference to this object, declare a private variable in the class in which you implement the solution event handlers.
        /// </remarks>
        ///-------------------------------------------------------------------------------------------------
        private SolutionEvents SolutionEvents { get; set; }

        public SolutionEventListener(TaskbarBuildProgressPackage package)
            : base(package)
        {
            SolutionEvents = package.IDE.Events.SolutionEvents;
            SolutionEvents.AfterClosing += SolutionEvents_AfterClosing;
        }

        internal event _dispSolutionEvents_OpenedEventHandler Opened;

        internal event _dispSolutionEvents_BeforeClosingEventHandler BeforeClosing;

        internal event _dispSolutionEvents_AfterClosingEventHandler AfterClosing;

        internal event _dispSolutionEvents_QueryCloseSolutionEventHandler QueryCloseSolution;

        internal event _dispSolutionEvents_RenamedEventHandler Renamed;

        internal event _dispSolutionEvents_ProjectAddedEventHandler ProjectAdded;

        internal event _dispSolutionEvents_ProjectRemovedEventHandler ProjectRemoved;

        internal event _dispSolutionEvents_ProjectRenamedEventHandler ProjectRenamed;

        private void SolutionEvents_AfterClosing()
        {
            if (AfterClosing != null)
            {
                AfterClosing();
            }
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                IsDisposed = true;

                if (disposing && SolutionEvents != null)
                {
                    SolutionEvents.AfterClosing -= SolutionEvents_AfterClosing;
                }
            }
        }
    }
}